package com.lupan.tomcatStudy.jndi.servlet;

import com.lupan.tomcatStudy.jndi.handle.MyHttpHandle;
import com.lupan.tomcatStudy.jndi.service.EmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 说明：
 *
 * @author lupan
 * @version 2016/6/27 12:34
 */
public class MyHttpServlet extends javax.servlet.http.HttpServlet {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    MyHttpHandle httpHandle = MyHttpHandle.getInstance();

    /**
     * GET请求调用
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.info("URL="+req.getRequestURL());
        logger.info("URI="+req.getRequestURI());
        String path = req.getPathInfo();
        logger.info("path="+path);

        switch (path) {
            case "/sendEmail":
                httpHandle.sendEmail(req, resp);
                break;
            case "/getDataSource":
                httpHandle.getDataSource(req, resp);
                break;
            case "/getBeanByJNDI":
                httpHandle.getBeanByJNDI(req, resp);
                break;
            default:
                throw new ServletException("this path has no handler to handle！");
        }
    }

    /**
     * POST请求调用
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
